package com.rjpacket.supernews.app;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.rjpacket.supernews.app.adapters.ImageViewPager;
import com.rjpacket.supernews.app.adapters.RecyclerViewAdapter;
import com.rjpacket.supernews.app.models.Recycle;
import com.rjpacket.supernews.app.models.RecycleCategory;
import com.rjpacket.supernews.app.utils.HttpTool;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;


public class RecycleViewActivity extends BaseActivity {
    //设置RecyclerView显示图片
    private RecyclerView recyclerView;

    private String string;
    //存储每一组相册分类里面图片的Url
    private List<String> imageSource;
    //存储缩略图url以及标题
    private LinkedList<List<String>> list;

    private ViewPager viewPager;

    private ImageViewPager adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_recycle_view);
        //获得上一个图片页传过来的url进行拼接图片地址
        Intent intent = getIntent();

        string = intent.getStringExtra("url");


        TextView actionBar = (TextView) findViewById(R.id.actionbar_title_img);

        actionBar.setText("美图秀秀");

        viewPager = (ViewPager) findViewById(R.id.img_viewpager);
        //设置瀑布流进行显示
        //recyclerView = (RecyclerView)findViewById(R.id.category_save_image_recycle);
        //StaggeredGridLayoutManager staggered = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        //LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //recyclerView.setLayoutManager(layoutManager);


        //Adapter显示的数据源
        //list = new LinkedList<HashMap<String,String>>();


        //存储所有图片资源的url
        imageSource = new LinkedList<String>();
        //拼接的网址
        String s = Constants.IMAGE_RECYCLE_URL_1
                + string
                + Constants.IMAGE_RECYCLE_URL_2;
        //异步任务解析接口的数据
        MyTask task1 = new MyTask();

        task1.execute(s);

        // viewPager.setAdapter(new ImageViewPager(this, imageSource));

        //创建图片显示的Adapter，用于在瀑布流中显示图片
        //adapter = new RecyclerViewAdapter(this,imageSource);

        //recyclerView.setAdapter(adapter);
    }

    /* //设置RecyclerView每一个条目的点击事件
     @Override
     public void onClick(View view) {
         int position = recyclerView.getChildPosition(view);
         RecycleCategory category = imageSource.get(position);
         Intent intent = new Intent(this,ImageDetail.class);
         intent.putExtra("url",category);
         startActivity(intent);
     }
 */
    //解析的异步任务
    private class MyTask extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... params) {

            String ret = null;

            if (params != null && params.length > 0) {
                String url = params[0];

                byte[] bytes = HttpTool.get(url);

                if (bytes != null) {

                    String data = new String(bytes, 0, bytes.length);
                    try {
                        //获得JSONObject进行解析
                        JSONObject jsonObject = new JSONObject(data);

                        JSONArray array = jsonObject.getJSONArray("images");

                        for (int i = 0; i < array.length(); i++) {

                            JSONObject object = array.getJSONObject(i);

                            Recycle recycle = new Recycle();

                            recycle.parseJson(object);
                            //所有详细图片的网址存储
                            imageSource.addAll(recycle.getList());
                            //存储解析数据传递给Adapter
                       /* HashMap<String,String> map = new HashMap<String, String>();
                        map.put("thumbUrl",recycle.getThumbUrl());
                        map.put("title",recycle.getTitle());
                        list.add();*/
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
            return ret;
        }

        @Override
        protected void onPostExecute(String s) {

            adapter = new ImageViewPager(RecycleViewActivity.this, imageSource);

            viewPager.setAdapter(adapter);
            //for (String s1 : imageSource) {
            // Log.d("-------",s1);
        }
        // Log.d("-----",imageSource+"");
    }
}

